﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBaoHanh.Template;
using BussinessLayer;
using MyCommon;
using DataAccessLayer;

namespace QLBaoHanh.QLDanhMuc
{
    public partial class frmNhanVienDetail : Form
    {
        private bool _isEditForm;
        private NhanVien _nhanVien;
        private int _nhanVienId;
        private frmDsNhanVien _callerForm;

        public int NhanVienId
        {
            set
            {
                _nhanVienId = value;
                //reload NhanVien
                _nhanVien = NhanVienBO.getInstance.GetNhanVienById(_nhanVienId);
                RefreshForm();
            }
        }

        public bool IsEditForm
        {
            set
            {
                _isEditForm = value;
                if (!_isEditForm)
                    FillFormNhanVien();
            }
        }

        public frmDsNhanVien CallerForm
        {
            get { return _callerForm; }
            set { _callerForm = value; }
        }

        public frmNhanVienDetail()
        {
            InitializeComponent();
            _isEditForm = false;
            _nhanVienId = 0;
            _nhanVien = null;
            cboSex.SelectedIndex = 0;
        }

        private void FillListAccount()
        {
            // Fill combobox nguoi dung
            NguoiDung BlankNguoiDung = new NguoiDung();
            BlankNguoiDung.Id = 0;
            BlankNguoiDung.Username = "";
            List<NguoiDung> ListNguoiDung = NguoiDungBO.getInstance.GetAllNguoiDungChuaSuDung().ToList();
            ListNguoiDung.Insert(0, BlankNguoiDung);
            if (_isEditForm && _nhanVien.NguoiDungId != null)
            {
                NguoiDung obj = NguoiDungBO.getInstance.GetNguoiDungById((int)_nhanVien.NguoiDungId);
                ListNguoiDung.Insert(1, obj);
            }
            cboAccount.DataSource = ListNguoiDung;
            cboAccount.DisplayMember = "Username";
            cboAccount.ValueMember = "Id";
            cboAccount.SelectedIndex = 0;
        }
        private void FillFormNhanVien()
        {
            FillListAccount();
            if (_isEditForm)
            {
                txtMaKH.Text = _nhanVien.Ma;
                txtName.Text = _nhanVien.Ten;
                cboSex.Text = _nhanVien.GioiTinh;
                dtpNgaySinh.Value = (DateTime)_nhanVien.NgaySinh;
                txtMobile.Text = _nhanVien.SoDienThoai;
                txtEmail.Text = _nhanVien.Email;
                txtAddress.Text = _nhanVien.DiaChi;
                if (_nhanVien.NguoiDung != null)
                {
                    cboAccount.Text = _nhanVien.NguoiDung.Username;
                }
                
            } 
            else
            {
                this.Text = "Thêm nhân viên";
                txtTitle.Text = "Thêm mới nhân viên";
                txtMaKH.Text = NhanVienBO.getInstance.GenerateMaNhanVien();
                txtName.Text = "";
                cboSex.SelectedIndex = 0;
                dtpNgaySinh.Value = DateTime.Today.AddYears(-20);
                txtMobile.Text = "";
                txtEmail.Text = "";
                txtAddress.Text = "";
            }
        }

        private bool IsDataHasChanged()
        {
            bool hasChange = false;
            //only check in case edit
            if(_isEditForm && _nhanVien != null)
            {
                if (  txtName.Text != _nhanVien.Ten
                   || txtEmail.Text != _nhanVien.Email
                   || txtAddress.Text != _nhanVien.DiaChi 
                   || cboSex.Text != _nhanVien.GioiTinh 
                   || (int)cboAccount.SelectedValue != ((_nhanVien.NguoiDungId == null)? 0: (int)_nhanVien.NguoiDungId))
                {
                    hasChange = true;
                }
            }
            else
            {
                if (txtName.Text != "" || txtEmail.Text != "" || txtAddress.Text !="")
                {
                    hasChange = true;
                }
            }
            return hasChange;
        }

        private bool SaveNhanVien()
        {
            if (ValidateFormData())
            {
                if (IsDataHasChanged())
                {
                    Nullable<int> NguoiDungId = 0;
                    NguoiDungId = (int)cboAccount.SelectedValue;
                    if(NguoiDungId == 0)
                    {
                        NguoiDungId = null;
                    }
                    if (_isEditForm)
                    {
                        //update  nhan vien
                        _nhanVien.Ten = txtName.Text.Trim();
                        _nhanVien.GioiTinh = cboSex.Text;
                        _nhanVien.NgaySinh = dtpNgaySinh.Value;
                        _nhanVien.SoDienThoai = txtMobile.Text.Trim();
                        _nhanVien.Email = txtEmail.Text.Trim();
                        _nhanVien.DiaChi = txtAddress.Text.Trim();
                        _nhanVien.NguoiDungId = NguoiDungId;
                        NhanVienBO.getInstance.UpdateNhanVien(_nhanVien);
                    } 
                    else
                    {
                        //insert nhan vien
                        _nhanVien = new NhanVien();
                        _nhanVien.isDelete = false;
                        _nhanVien.Ma = txtMaKH.Text.Trim();
                        _nhanVien.Ten = txtName.Text.Trim();
                        _nhanVien.GioiTinh = cboSex.Text;
                        _nhanVien.NgaySinh = dtpNgaySinh.Value;
                        _nhanVien.SoDienThoai = txtMobile.Text.Trim();
                        _nhanVien.Email = txtEmail.Text.Trim();
                        _nhanVien.DiaChi = txtAddress.Text.Trim();
                        _nhanVien.NguoiDungId = NguoiDungId;
                        NhanVienBO.getInstance.InsertNhanVien(_nhanVien);
                        _callerForm.RefreshForm();
                    }
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        private bool ValidateFormData()
        {
            //hien thi message du lieu ko valid
            return true;
        }

        private void RefreshForm()
        {
            FillFormNhanVien();
        }

        private void frmNhanVienDetail_Load(object sender, EventArgs e)
        {
            //Neu la edit thi load form edit khong thi load form them moi
            if (_isEditForm)
            {
                if(_nhanVien == null)
                {
                    return;
                }
            }
 
            FillFormNhanVien();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (SaveNhanVien())
            {
                this.Close();
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            RefreshForm();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if(IsDataHasChanged())
            {
                if (MessageBox.Show("Dữ liệu có sự thay đổi, Bạn có muốn lưu không?"
                , "Đóng", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    //save data
                    if (SaveNhanVien())
                    {
                        this.Close();
                    }
                }
                else
                {
                    //close form
                    this.Close();    
                }
            }
            else
            {
                //close form
                this.Close();
            }
        }
    }
}
